/*******************************************************************************
  * Copyright(c) 2023 Levetop Semiconductor Co.,Led. All rights reserved.
  * @file     multiVarButton.c
  * @author   UartTFT Application Team
  * @version  V0.0.1
  * @date     2023-01-04
  * @brief    This file contains the functions prototypes for the UartTFT
  *           Application display firmware.
 ********************************************************************************/
#include "multiVarButton.h"
#include "bsp.h"
#include "levetop.h"
#include "uiIcon.h"

/*variable control of touch mode*/
uint8_t VarKey_count = 0;
uint8_t VarKey_flag = 0;
uint8_t VarKey_num = 0;
VarKey_Info gVarKey_Info[BATCH_SIZE];

/***********************************************************************************
 * Function Name : VarKey_touch
 * Description   : Touch detection of multivariable adjustment.
 * Input         : None
 * Output        : None
 * Return        : None
 ************************************************************************************/
void VarKey_touch(void)
{
	uint8_t i = 0;
	uint8_t rebuf[50],buff1[12];
	uint8_t page_flag = 0;
	uint16_t w,h,flag;

	// press down
	if (gTpInfo.sta) // press down
	{
		for (i = 0; i < VarKey_count; i++)
		{
			if (gTpInfo.x[0] >= gVarKey_Info[i].Xs && gTpInfo.y[0] >= gVarKey_Info[i].Ys &&
				gTpInfo.x[0] <= gVarKey_Info[i].Xe && gTpInfo.y[0] <= gVarKey_Info[i].Ye &&
				VarKey_flag == 0 && button_Press_flag == 0)
			{
				if (gVarKey_Info[i].press_id != 0xFFFF)
				{
					if (gVarKey_Info[i].overlay == 0xFFFF)
						Display_Icon(gVarKey_Info[i].press_id, gVarKey_Info[i].Xs, gVarKey_Info[i].Ys, 0);
					else
					{
						LT_ReadFlash_UI(buff1, addr_index[10] + 12 * gVarKey_Info[i].press_id  + multi_language * 12 * addr_index[11], 12);
						pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
						w = buff1[4] + (buff1[5] << 8);
						h = buff1[6] + (buff1[7] << 8);
						pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
						flag = buff1[11];


						if (flag == 1 || flag == 2)
							LT268_BG_to_buffer(0, 0, w, m_PngData,gVarKey_Info[i].Xs, gVarKey_Info[i].Ys,w, h);

						LT268_pic_to_buffer(0,0,w,m_PngData,
											0,0,w,pic_add,
											w,h,flag);

						Get_date_variable_Id_overlay(pic_id,gVarKey_Info[i].overlay,gVarKey_Info[i].Xs,gVarKey_Info[i].Ys,w,h);


						LT268_buffer_to_TFT(0,0,w,m_PngData,
								gVarKey_Info[i].Xs,gVarKey_Info[i].Ys,w,h);
					}
				}
				VarKey_num = i;
				VarKey_flag = 1;
				button_Press_flag = 1;
				if (buzzer)
					touch_buzzer();
			}
		}
	}

	if (Button_Key_Code_Flag == 1)
	{
		for (i = 0; i < VarKey_count; i++)
		{
			if (gVarKey_Info[i].Xs == 0xFFFF && gVarKey_Info[i].Ys ==  Button_Key_Code)
			{
				Button_Key_Code_Flag = 0;
				Button_Key_Code = 0;
				VarKey_num = i;
				for (i = 0; i < 8; i++)
				{
					if (gVarKey_Info[VarKey_num].var_addr[i] != 0xFFFF && gVarKey_Info[VarKey_num].var_addr[i] <= VAR_ADDR_E)
					{
						var[gVarKey_Info[VarKey_num].var_addr[i] * 2] = (gVarKey_Info[VarKey_num].var[i] >> 8) & 0xFF;
						var[gVarKey_Info[VarKey_num].var_addr[i] * 2 + 1] = gVarKey_Info[VarKey_num].var[i] & 0xFF;

						if (gVarKey_Info[VarKey_num].var_addr[i] > VAR_ADDR_S)
							reg_operation(gVarKey_Info[VarKey_num].var_addr[i]);
						if (gVarKey_Info[VarKey_num].var_addr[i] == VAR_ADDR_S)
							page_flag = 1;
					}
				}
				Get_date_variable_Id_Regular_update(pic_id);

				if (gVarKey_Info[VarKey_num].Code == 0xC00B)
				{
					rebuf[3] = 0x41;
					for (i = 0; i < 8; i++)
					{
						rebuf[4 * i + 4] = gVarKey_Info[VarKey_num].var_addr[i] >> 8;
						rebuf[4 * i + 5] = gVarKey_Info[VarKey_num].var_addr[i] & 0xFF;
						rebuf[4 * i + 6] = gVarKey_Info[VarKey_num].var[i] >> 8;
						rebuf[4 * i + 7] = gVarKey_Info[VarKey_num].var[i] & 0xFF;
						Var_AddrToReg(&rebuf[4 * i + 1]);
					}
					LT_SendData_CRC_Frame(rebuf, 33);
				}

				if (page_flag == 1) reg_operation(VAR_ADDR_S);

				if (gVarKey_Info[VarKey_num].Next_id != 0xFFFF)
					Display_page(gVarKey_Info[VarKey_num].Next_id);
				break;
			}
		}
	}

	// raise
	if (gTpInfo.sta == 0 && VarKey_flag == 1)
	{
		if (gVarKey_Info[VarKey_num].id != 0xFFFF)
		{
			if (gVarKey_Info[VarKey_num].overlay == 0xFFFF)
				Display_Icon(gVarKey_Info[VarKey_num].id, gVarKey_Info[VarKey_num].Xs, gVarKey_Info[VarKey_num].Ys, 0);
			else
			{
				LT_ReadFlash_UI(buff1, addr_index[10] + 12 * gVarKey_Info[VarKey_num].id  + multi_language * 12 * addr_index[11], 12);
				pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
				w = buff1[4] + (buff1[5] << 8);
				h = buff1[6] + (buff1[7] << 8);
				pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
				flag = buff1[11];


				if (flag == 1 || flag == 2)
					LT268_BG_to_buffer(0, 0, w, m_PngData,gVarKey_Info[VarKey_num].Xs, gVarKey_Info[VarKey_num].Ys,w, h);

				LT268_pic_to_buffer(0,0,w,m_PngData,
									0,0,w,pic_add,
									w,h,flag);

				Get_date_variable_Id_overlay(pic_id,gVarKey_Info[VarKey_num].overlay,gVarKey_Info[VarKey_num].Xs,gVarKey_Info[VarKey_num].Ys,w,h);


				LT268_buffer_to_TFT(0,0,w,m_PngData,
						gVarKey_Info[VarKey_num].Xs,gVarKey_Info[VarKey_num].Ys,w,h);
			}
		}
		button_Press_flag = 0;
		VarKey_flag = 0;

		for (i = 0; i < 8; i++)
		{
			if (gVarKey_Info[VarKey_num].var_addr[i] != 0xFFFF && gVarKey_Info[VarKey_num].var_addr[i] <= VAR_ADDR_E)
			{
				var[gVarKey_Info[VarKey_num].var_addr[i] * 2] = (gVarKey_Info[VarKey_num].var[i] >> 8) & 0xFF;
				var[gVarKey_Info[VarKey_num].var_addr[i] * 2 + 1] = gVarKey_Info[VarKey_num].var[i] & 0xFF;

				if (gVarKey_Info[VarKey_num].var_addr[i] > VAR_ADDR_S)
					reg_operation(gVarKey_Info[VarKey_num].var_addr[i]);
				else if (gVarKey_Info[VarKey_num].var_addr[i] == VAR_ADDR_S)
					page_flag = 1;
			}
		}
		Get_date_variable_Id_Regular_update(pic_id);

		if (gVarKey_Info[VarKey_num].Code == 0xC00B)
		{
			rebuf[3] = 0x41;
			for (i = 0; i < 8; i++)
			{
				rebuf[4 * i + 4] = gVarKey_Info[VarKey_num].var_addr[i] >> 8;
				rebuf[4 * i + 5] = gVarKey_Info[VarKey_num].var_addr[i] & 0xFF;
				rebuf[4 * i + 6] = gVarKey_Info[VarKey_num].var[i] >> 8;
				rebuf[4 * i + 7] = gVarKey_Info[VarKey_num].var[i] & 0xFF;
				Var_AddrToReg(&rebuf[4 * i + 1]);
			}
			LT_SendData_CRC_Frame(rebuf, 33);
		}

		if (page_flag == 1) reg_operation(VAR_ADDR_S);

		if (gVarKey_Info[VarKey_num].Next_id != 0xFFFF)
			Display_page(gVarKey_Info[VarKey_num].Next_id);
		else
			page_date_update(pic_id);
	}
}

void VarKey_touch_overlay(uint16_t id, uint8_t num)
{
	uint16_t w, h, flag, icon_id;
	uint8_t buff1[12];
	if(gVarKey_Info[num].overlay != 0xFFFF)
	{
		if(Get_date_variable_Id_overlay_update(id,gVarKey_Info[num].overlay))
		{
			if(VarKey_flag == 1 && VarKey_num == num) icon_id = gVarKey_Info[num].press_id;
			else icon_id = gVarKey_Info[num].id;

			if (icon_id == 0xFFFF)
				icon_id = gVarKey_Info[num].id;

			if(icon_id != 0xFFFF)
			{
				LT_ReadFlash_UI(buff1, addr_index[10] + 12 * icon_id  + multi_language * 12 * addr_index[11], 12);
				pic_add = (buff1[0]) + (buff1[1] << 8) + (buff1[2] << 16) + (buff1[3] << 24);
				w = buff1[4] + (buff1[5] << 8);
				h = buff1[6] + (buff1[7] << 8);
				pic_len = buff1[8] + (buff1[9] << 8) + (buff1[10] << 16);
				flag = buff1[11];


				if (flag == 1 || flag == 2)
					LT268_BG_to_buffer(0, 0, w, m_PngData,gVarKey_Info[num].Xs, gVarKey_Info[num].Ys,w, h);

				LT268_pic_to_buffer(0,0,w,m_PngData,
									0,0,w,pic_add,
									w,h,flag);

				Get_date_variable_Id_overlay(pic_id,gVarKey_Info[num].overlay,gVarKey_Info[num].Xs,gVarKey_Info[num].Ys,w,h);


				LT268_buffer_to_TFT(0,0,w,m_PngData,
						gVarKey_Info[num].Xs,gVarKey_Info[num].Ys,w,h);
			}
		}
	}
}
